package com.ybc.chinese.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ybc.chinese.R;
import com.ybc.chinese.bean.FeedBack;
import com.ybc.chinese.utils.CommonUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * <p>反馈<p/>
 * Created by Yubch on 17/2/15.
 */
public class FeedBackActivity extends BaseActivity {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_left)
    ImageView ivLeft;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.et_title)
    EditText etTitle;
    @Bind(R.id.et_content)
    EditText etContent;

    private Context mCtx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        mCtx = this;
        ButterKnife.bind(this);
        tvTitle.setText(getString(R.string.feedback));
        ivLeft.setImageResource(R.mipmap.ic_back);
        ivLeft.setVisibility(View.VISIBLE);
        tvRight.setText(getString(R.string.send));
        tvRight.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.iv_left)
    public void left() {
        finish();
    }

    @OnClick(R.id.tv_right)
    public void right() {
        String title = etTitle.getText().toString();
        String content = etContent.getText().toString();
        if(TextUtils.isEmpty(title)) {
            CommonUtil.showToast(mCtx, getString(R.string.title_is_empty));
            return;
        }
        if(TextUtils.isEmpty(content)) {
            CommonUtil.showToast(mCtx, getString(R.string.content_is_empty));
            return;
        }
        FeedBack feedBack = new FeedBack();
        feedBack.setTitle(title);
        feedBack.setContent(content);
        showRunningDialog(R.string.sending);
        feedBack.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                dismissRunningDialog();
                if(e == null) {
                    CommonUtil.showToast(mCtx, "反馈成功");
                    finish();
                } else {
                    CommonUtil.showToast(mCtx, "反馈失败,请稍后再试");
                }
            }
        });
    }
}
